package com.cryptoneedle.center.source.service.crud;

import com.cryptoneedle.center.source.entity.metadata.Catalog;
import com.cryptoneedle.center.source.entity.metadata.Database;
import com.cryptoneedle.center.source.entity.metadata.Table;
import com.cryptoneedle.center.source.repository.constraint.*;
import com.cryptoneedle.center.source.repository.enhance.DimensionRepository;
import com.cryptoneedle.center.source.repository.metadata.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>description:  </p>
 *
 * @author CryptoNeedle
 * @date 2025-10-23
 */
@Slf4j
@Service
@Transactional
public class PatchDataService {

    @Autowired
    private FoundDataService select;
    @Autowired
    private AddDataService add;
    @Autowired
    private PatchDataService patch;
    @Autowired
    private SaveDataService save;
    @Autowired
    private DeleteDataService delete;

    @Resource
    private ServerRepository serverRepository;
    @Resource
    private CatalogRepository catalogRepository;
    @Resource
    private DatabaseRepository databaseRepository;
    @Resource
    private TableRepository tableRepository;
    @Resource
    private ColumnRepository columnRepository;

    @Resource
    private ConstraintCheckRepository constraintCheckRepository;
    @Resource
    private ConstraintForeignRepository constraintForeignRepository;
    @Resource
    private ConstraintPrimaryRepository constraintPrimaryRepository;
    @Resource
    private ConstraintUniqueRepository constraintUniqueRepository;
    @Resource
    private IndexUniqueRepository indexUniqueRepository;

    @Resource
    private DimensionRepository dimensionRepository;

    /**
     * ServerRepository
     */

    /**
     * CatalogRepository
     */
    public void catalogEnabled(String catalogName, boolean enabled) {
        Catalog catalog = select.catalog(catalogName);
        if (catalog.getEnabled() != enabled) {
            catalog.setEnabled(enabled);
            save.save(catalog);
        }
    }

    public void catalogDefaultSystemCode(String catalogName, String defaultSystemCode) {
        Catalog catalog = select.catalog(catalogName);
        String oldSystemCode = catalog.getDefaultSystemCode();

        if (!Strings.CS.equals(oldSystemCode, defaultSystemCode)) {
            catalog.setDefaultSystemCode(defaultSystemCode);
            save.save(catalog);

            List<Database> databases = select.databases(catalog);
            for (Database database : databases) {
                if (Strings.CS.equals(oldSystemCode, database.getDefaultSystemCode())) {
                    patch.databaseDefaultSystemCode(catalogName, database.getDatabaseName(), defaultSystemCode);
                }
            }
        }
    }

    /**
     * DatabaseRepository
     */
    public void databaseEnabled(String catalogName, String databaseName, boolean enabled) {
        Database database = select.database(catalogName, databaseName);
        if (database.getEnabled() != enabled) {
            database.setEnabled(enabled);
            databaseRepository.save(database);
        }
    }

    public void databaseDefaultSystemCode(String catalogName, String databaseName, String defaultSystemCode) {
        Database database = select.database(catalogName, databaseName);
        String oldSystemCode = database.getDefaultSystemCode();

        if (!Strings.CS.equals(oldSystemCode, defaultSystemCode)) {
            database.setDefaultSystemCode(defaultSystemCode);
            save.save(database);

            List<Table> tables = select.tables(catalogName, databaseName);
            for (Table table : tables) {
                if (Strings.CS.equals(oldSystemCode, table.getSystemCode())) {
                    patch.tableSystemCode(catalogName, database.getDatabaseName(), table.getTableName(), defaultSystemCode);
                }
            }
        }
    }

    /**
     * TableRepository
     */
    public void tableEnabled(String catalogName, String databaseName, String tableName, boolean enabled) {
        Table table = select.table(catalogName, databaseName, tableName);
        if (table.getEnabled() != enabled) {
            table.setEnabled(enabled);
            tableRepository.save(table);
        }
    }

    public void tableSystemCode(String catalogName, String databaseName, String tableName, String systemCode) {
        Table table = select.table(catalogName, databaseName, tableName);
        if (!Strings.CS.equals(table.getSystemCode(), systemCode)) {
            table.setSystemCode(systemCode);
            save.save(table);
        }
    }

    /**
     * ColumnRepository
     */

    /**
     * ConstraintCheckRepository
     */

    /**
     * ConstraintForeignRepository
     */

    /**
     * ConstraintPrimaryRepository
     */

    /**
     * ConstraintUniqueRepository
     */

    /**
     * IndexUniqueRepository
     */

    /**
     * DimensionRepository
     */
}